package org.gjy.m8.concurrent;

import com.google.common.collect.Lists;

import java.util.List;
import java.util.concurrent.Semaphore;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author gjy
 * @version 1.0
 * @since 2025-07-03 15:07:32
 */
public class PolicyTest {

    public static void main(String[] args) {
        ThreadPoolExecutor orderPool = ThreadPoolUtil.getOrderPool();
        List<Integer> list = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
        Semaphore semaphore = new Semaphore(3);
        for (Integer i : list) {
            try {
                if (semaphore.availablePermits() == 0) {
                    System.out.println("任务繁忙");
                }
                semaphore.acquire();
                orderPool.execute(new DbRunnable(i, semaphore));
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        orderPool.shutdown();
    }

    private static void extracted() {
        ThreadPoolExecutor orderPool = ThreadPoolUtil.getOrderPool();
        List<Integer> list = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
        for (Integer i : list) {
            orderPool.execute(new DbRunnable(i));
        }
        orderPool.shutdown();
    }
}
